﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MVCTippSpielManagement.Management.Interfaces;
using MVCTippSpielManagement.Management;
using MVCTippSpielManagement.Management.Model;

namespace MVCTippSpielView.View
{
	public partial class Hauptapplikation : Form, IView
	{
		private IViewAdapter _Controller;

		public Hauptapplikation()
		{
			InitializeComponent();
		}

		#region IView Member

		public void SetController(IViewAdapter a)
		{
			_Controller = a;
		}

		public void ViewFactory()
		{
			UserMenu mnuItems = (UserMenu)_Controller.ModelFactory("GetMainMenuItems");

			foreach (MyMenuItem mi in mnuItems.Items)
			{
				ToolStripMenuItem mnuItem = new ToolStripMenuItem();
				//MenuItem mnuItem = new MenuItem();
				mnuItem.Text = mi.Text;
				mnuItem.Name = mi.Name;
				mnuItem.ShortcutKeyDisplayString = mi.ShortCut;
				if (mi.MenuItems.Count > 0)
				{
					FillMenu(mnuItem, mi.MenuItems);
				}
				mnuMain.Items.Add(mnuItem);
			}

			this.ShowDialog();
		}

		private void FillMenu(ToolStripMenuItem _mi, List<MyMenuItem> liMi)
		{
			foreach (MyMenuItem mi in liMi)
			{
				ToolStripMenuItem mnuItem = new ToolStripMenuItem();
				mnuItem.Text = mi.Text;
				mnuItem.Name = mi.Name;
				mnuItem.ShortcutKeyDisplayString = mi.ShortCut;
				mnuItem.Click += new EventHandler(MenuItemClick);
				_mi.DropDownItems.Add(mnuItem);
				if (mi.MenuItems.Count > 0)
				{
					FillMenu(mnuItem, mi.MenuItems);
				}
				mnuMain.Items.Add(_mi);
			}
		}
		#endregion

		public void MenuItemClick(object sender, EventArgs e)
		{
			if (sender is ToolStripMenuItem)
			{
				ToolStripMenuItem myTs = (ToolStripMenuItem)sender;

				switch (myTs.Name)
				{
					case "mnuFileRegister":
						Controller c = new Controller(new ViewTest(this), new ModelTest());
						c.ViewFactory();
						break;
					case "mnuFileClose":
						Application.Exit();
						break;
				}
			}
		}
	}
}
